const userApi = require("../../api/user.js");
const homeApi = require("../../api/home.js");
Component({
  properties: {
    userId: { type: String, value: "" }, //自己还是别人的userId
    profileType: { type: String, value: "my" }, //my是自己，other是别人
  },
  data: {
    hidden: true,
    loadingData: false, //数据是否正在加载中，避免数据多次加载
    selected: 0,
    list: ["个人名片", "企业介绍", "动态"],
    nameCardData: undefined,
    businessCardData: [],
    communityList: [],
    popImage: "",
    showPopBack: false,
    unReadMessageCount: 0,
    isEmpty: false,
    bottomPadding: 0
  },
  observers: {
    userId: function (val) {
      if (val) {
        this.getPersonalCardInfo(); //获取看片信息
        this.getUnReadMessageCount(); //未读消息数量
      }
    },
  },
  pagelifetimes: {
    async show() {
      let bottomPadding = getApp().globalData.bottomPadding;
      this.setData({ bottomPadding })
    },
  },
  methods: {
    refreshData() {
      this.getUnReadMessageCount();
      if (this.data.selected === 2) {
        let community = this.selectComponent("#community");
        community.getUnReadMessageCount();
      }
    },
    async getPersonalCardInfo() {
      let res = await userApi.getPersonalCard({ userId: this.data.userId });
      if (res.code === 200) {
        let data = res.data;
        data.personalInfo = JSON.parse(data.personalInfo);
        if (!data.personalInfo) {
          data.personalInfo = [];
        }
        this.setData({ nameCardData: data });
      }
    },
    pullDownRefresh() {
      let community = this.selectComponent("#community");
      if (community) {
        community.pullDownRefresh();
      }
    },
    reachBottom() {
      let community = this.selectComponent("#community");
      if (community) {
        community.reachBottom();
      }
    },
    async getUnReadMessageCount() {
      if (this.data.userId === wx.getStorageSync("loginInfo").userId) {
        let res = await userApi.getUnReadMessageCount({
          userId: this.data.userId,
        });
        this.setData({
          unReadMessageCount: res.data.count,
          avatarUrl: res.data?.avatarUrl,
        });
      } else {
        this.setData({
          unReadMessageCount: 0,
        });
      }
    },
    openLocation(e) {
      wx.openLocation({
        latitude: parseFloat(e.currentTarget.dataset.info.latitude || 0),
        longitude: parseFloat(e.currentTarget.dataset.info.longitude || 0),
        name: e.currentTarget.dataset.info.detailAddress, //位置名
        address: e.currentTarget.dataset.info.addressDes, //地址的详细说明
      });
    },
    makePhoneCall(e) {
      wx.makePhoneCall({ phoneNumber: e.currentTarget.dataset.tel });
    },
    //图片预览
    previewImage(e) {
      let list = e.currentTarget.dataset.src;
      wx.previewImage({
        current: list[0], // 当前显示图片的链接/路径
        urls: list[1], // 需要预览的图片链接/路径列表
      });
    },
    //接受子项目传来的圈子的数量
    getCommunityNum(e) {
      this.setData({
        isEmpty:
          e.detail.unReadMessageCount === 0 && e.detail.communityList === 0,
      });
    },
    async selectedTab(e) {
      let index = e.currentTarget.dataset.index;
      this.setData({ selected: index });
      if (index === 1) {
        let res = await homeApi.getEnterpriseCard({ userId: this.data.userId });
        this.setData({
          businessCardData: res.data ? JSON.parse(res.data.enterpriseInfo) : [],
        });
      }
    },
    closePopBack() {
      this.setData({ showPopBack: false });
    },
    clickMediaItem(e) {
      let type = e.currentTarget.dataset["type"];
      switch (type) {
        case "wx":
          if (this.data.nameCardData.wechatQrCode) {
            this.setData({
              popImage: this.data.nameCardData.wechatQrCode,
              showPopBack: true,
            });
          } else if (this.data.nameCardData.wechatNo) {
            wx.setClipboardData({ data: this.data.nameCardData.wechatNo });
          } else {
            wx.showToast({ title: "暂无数据", icon: "none", duration: 2000 });
          }
          break;
        case "video":
          if (this.data.nameCardData.videoQrCode) {
            this.setData({
              popImage: this.data.nameCardData.videoQrCode,
              showPopBack: true,
            });
          } else if (this.data.nameCardData.videoNo) {
            wx.setClipboardData({ data: this.data.nameCardData.videoQrCode });
          } else {
            wx.showToast({ title: "暂无数据", icon: "none", duration: 2000 });
          }
          break;
        case "ks":
          if (this.data.nameCardData.kwai) {
            wx.setClipboardData({ data: this.data.nameCardData.kwai });
          } else {
            wx.showToast({ title: "暂无数据", icon: "none", duration: 2000 });
          }
          break;
        case "dy":
          if (this.data.nameCardData.tiktok) {
            wx.setClipboardData({ data: this.data.nameCardData.tiktok });
          } else {
            wx.showToast({ title: "暂无数据", icon: "none", duration: 2000 });
          }
          break;
        default:
          break;
      }
    },
    articleClick(e) {
      wx.navigateTo({
        url: `/pages/community/detail/index?articleId=${e.currentTarget.dataset.item.id}`,
      });
    },
  },
});
